//
//  LZMyRecordRestaurantsTableView.m
//  laziz_Consumer
//
//  Created by xbn_iMac on 2016/12/8.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZMyRecordRestaurantsTableView.h"
#import "UITableView+FDTemplateLayoutCell.h"
#import "UITableView+LZTableView.h"
#import "LZMyFavoriteModel.h"
#import "LZRestaurantListModel.h"
#import "LZZanAndCollectStatusPresenter.h"
#import "LZNoDataPublicView.h"
#import "Masonry.h"

@interface LZMyRecordRestaurantsTableView ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) NSMutableArray *dataArr;

@property (nonatomic, assign) int page;
@property (nonatomic, assign) int limit;

@end

@implementation LZMyRecordRestaurantsTableView

-(instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
    self = [super initWithFrame:frame style:style];
    if (self) {
        self.separatorColor = [UIColor clearColor];
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.backgroundColor = UIColorFromRGB(0xeeeeee, 1);
        [self registerCell];
        [self configBaseData];
        self.delegate = self;
        self.dataSource = self;
    }
    return self;
}

- (void)dealloc {
    [LZZanAndCollectStatusPresenter shareInstance].isStartRecordRestaurantCollectStatus = NO;
}

- (void)checkHaveData {
    if (self.dataArr.count == 0) {
        [LZZanAndCollectStatusPresenter shareInstance].isStartRecordRestaurantCollectStatus = YES;
        if (self.subTableViewDelegate && [self.subTableViewDelegate respondsToSelector:@selector(requestDateWithTag:andPrameter:andPage:andLimit:resultSuccess:)]) {
            if (self.mj_footer.state != MJRefreshStateRefreshing) {
                [self.mj_footer beginRefreshing];
            }
        }
    }
    else {
        if (self.type == MyRecordsType_Favorites) {
            // 为了保持数据的统一性，做一下视频是否收藏的校验
            BOOL isRefresh = NO;
            for (LZCollectStatusModel *statusModel in [LZZanAndCollectStatusPresenter shareInstance].collectRestaurantArray) {
                NSArray *result = [self.dataArr filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self.restaurantDTO.restaurantId = %@",statusModel.collectId]];
                if (result.count>0) {
                    LZMyFavoriteModel *favoriteModel = result[0];
                    LZRestaurantListModel *model = favoriteModel.restaurantDTO;
                    if (model.isStore != statusModel.isStore) {
                        isRefresh = YES;
                        model.isStore = statusModel.isStore;
                    }
                }
            }
            if (isRefresh) {
                [self reloadData];
            }
        }
    }
}

#pragma mark - configure Super Refresh method

- (void)upLoadMoreData {
    [self requestList];
}

- (void)requestList {
    
    WS(weakSelf)
    
    NSInteger removeCount = 0;
    
    if (self.type == MyRecordsType_Favorites) {
        if (self.dataArr.count>0) {
            NSArray *result = [self.dataArr filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self.restaurantDTO.isStore = NO"]];
            removeCount = result.count;
        }
    }
    
    NSDictionary *prameter = @{@"removeCnt":[NSString stringWithFormat:@"%ld",removeCount]};
    
    [self.subTableViewDelegate requestDateWithTag:2 andPrameter:prameter andPage:self.page andLimit:self.limit resultSuccess:^(NSArray *result,NSString *timestamp) {

        [weakSelf.mj_footer endRefreshing];
        
        if (result) {
            weakSelf.page ++;
            
            if (result.count>0) {
                [weakSelf.dataArr addObjectsFromArray:result];
                [weakSelf reloadData];
            }
            else {
                if (weakSelf.dataArr.count == 0) {
                    [LZNoDataPublicView showNoDataViewInView:weakSelf];
                    [weakSelf setNullStringFooterText];
                }
                else {
                    [LZNoDataPublicView hiddenNoDataViewInView:weakSelf];
                    [weakSelf setNoMoreDataFooterText];
                }
                
                [weakSelf.mj_footer endRefreshingWithNoMoreData];
            }
        }
    }];
}

#pragma mark - Private Method

- (void)registerCell {
    [self registerNib:[UINib nibWithNibName:@"LZRestaurantTableViewCell" bundle:nil] forCellReuseIdentifier:@"LZRestaurantTableViewCell"];
}

- (void)configBaseData {
    self.page = 1;
    self.limit = 10;
    self.dataArr = [NSMutableArray array];
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LZRestaurantTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LZRestaurantTableViewCell"];
    if (self.type == MyRecordsType_ToBeReviewed) {
        cell.type = CellType_MyToBeReviewed;
    }
    else if (self.type == MyRecordsType_Favorites) {
        cell.type = CellType_MyFavorites;
    }
    
    if (self.dataArr.count>indexPath.row) {
        LZMyFavoriteModel *model = self.dataArr[indexPath.row];
        cell.model = model.restaurantDTO;
    }
    
    cell.delegate = self.clickButtonDelegate;
    cell.indexPath = indexPath;
    return cell;
}


#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [tableView fd_heightForCellWithIdentifier:@"LZRestaurantTableViewCell" cacheByIndexPath:indexPath configuration:^(id cell) {
        
    }];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (self.restaurantTableDelegate && [self.restaurantTableDelegate respondsToSelector:@selector(restaurantsBaseTableViewDidSelectRowAtIndexPath:object:)]) {
        LZMyFavoriteModel *model = self.dataArr[indexPath.row];
        [self.restaurantTableDelegate restaurantsBaseTableViewDidSelectRowAtIndexPath:indexPath object:model.restaurantDTO];
    }
}

@end
